<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="受益人姓名" prop="syName">
        <el-input
          v-model="queryParams.syName"
          placeholder="请输入受益人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['guarantee:order:add']"
        >新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <span>{{ statusDict[scope.row.status] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="受益人姓名" align="center" prop="syName" />
      <el-table-column label="被保证人姓名" align="center" prop="bbzrName" />
      <el-table-column label="保函类型" align="center" prop="bhType" />
      <el-table-column label="保函金额" align="center" prop="bhMoney" />
      <el-table-column label="项目名称" align="center" prop="xmName" />
      <el-table-column label="担保期限" align="center" prop="dbQixian" />
      <el-table-column label="中标时间或合同签署时间" align="center" prop="xmDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工期" align="center" prop="xmGongqi" />
      <el-table-column label="出函银行" align="center" prop="xmChBank" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status == 0||scope.row.status == -1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['guarantee:order:edit']"
          >修改</el-button>
          <el-button v-if="scope.row.status == 0||scope.row.status == -1"
                     size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['guarantee:order:remove']"
          >删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-tickets" @click="openDetail(scope.row)"
          >详情</el-button>
          <el-button v-if="scope.row.status == 0||scope.row.status == -1" size="mini" type="text" icon="el-icon-edit"
                     @click="handleTijiao(scope.row)" >提交</el-button>
          <el-button v-if="scope.row.status == 4" size="mini" type="text" icon="el-icon-tickets" @click="handleZhuan(scope.row)"
          >转入</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改询单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1360px" append-to-body>
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-width="180px">
        <el-form-item label="受益人姓名" prop="syName">
          <el-input v-model="form.syName" placeholder="请输入受益人姓名" />
        </el-form-item>
        <el-form-item label="受益人企业性质" prop="xmSyType" :rules="{ required: false, message: '请选择', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.syQyxz" placeholder="请选择受益人企业性质">
            <el-option label="民营" value="民营"></el-option>
            <el-option label="上市公司" value="上市公司"></el-option>
            <el-option label="国有" value="国有"></el-option>
            <el-option label="代理公司" value="代理公司"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="被保证人姓名" prop="bbzrName">
          <el-input v-model="form.bbzrName" placeholder="请输入被保证人姓名" />
        </el-form-item>
        <el-form-item label="被保证人企业性质" prop="bbzrQyxz" :rules="{ required: false, message: '请选择', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.bbzrQyxz" placeholder="请选择被保证人企业性质">
            <el-option label="施工企业" value="施工企业"></el-option>
            <el-option label="生产企业" value="生产企业"></el-option>
            <el-option label="贸易服务" value="贸易服务"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="被保证人注册资金" prop="bbzrZczj">
          <el-input v-model="form.bbzrZczj" placeholder="请输入被保证人注册资金" />
        </el-form-item>
        <el-form-item label="被保证人成立时间" prop="bbzrDate">
          <el-date-picker clearable
                          v-model="form.bbzrDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择被保证人成立时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="对应资质" prop="dyzz" :rules="{ required: false, message: '请选择', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.dyzz" placeholder="请选择对应资质">
            <el-option label="总一" value="总一"></el-option>
            <el-option label="总二" value="总二"></el-option>
            <el-option label="总三" value="总三"></el-option>
            <el-option label="专一" value="专一"></el-option>
            <el-option label="专二" value="专二"></el-option>
            <el-option label="专三" value="专三"></el-option>
            <el-option label="劳务不分等级" value="劳务不分等级"></el-option>
            <el-option label="高精特新" value="高精特新"></el-option>
            <el-option label="自有场地" value="自有场地"></el-option>
            <el-option label="租赁场地" value="租赁场地"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最高资质" prop="zgzz" :rules="{ required: false, message: '请选择', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.zgzz" placeholder="请选择最高资质">
            <el-option label="总一" value="总一"></el-option>
            <el-option label="总二" value="总二"></el-option>
            <el-option label="总三" value="总三"></el-option>
            <el-option label="专一" value="专一"></el-option>
            <el-option label="专二" value="专二"></el-option>
            <el-option label="专三" value="专三"></el-option>
            <el-option label="劳务不分等级" value="劳务不分等级"></el-option>
            <el-option label="高精特新" value="高精特新"></el-option>
            <el-option label="自有场地" value="自有场地"></el-option>
            <el-option label="租赁场地" value="租赁场地"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保函类型" prop="bhType" :rules="{ required: false, message: '请选择', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.bhType" placeholder="请选择保函类型">
            <el-option label="履约" value="履约"></el-option>
            <el-option label="预付款" value="预付款"></el-option>
            <el-option label="民工" value="民工"></el-option>
            <el-option label="质保" value="质保"></el-option>
            <el-option label="支付" value="支付"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保函格式" prop="bhGs" :rules="{ required: false, message: '请选择', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.bhGs" placeholder="请选择保函格式">
            <el-option label="独立" value="独立"></el-option>
            <el-option label="独立付条件" value="独立付条件"></el-option>
            <el-option label="一般" value="一般"></el-option>
            <el-option label="连带" value="连带"></el-option>
            <el-option label="无要求" value="无要求"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保函金额" prop="bhMoney">
          <el-input v-model="form.bhMoney" placeholder="请输入保函金额" />
        </el-form-item>
        <el-form-item label="项目名称" prop="xmName">
          <el-input v-model="form.xmName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="担保期限" prop="dbQixian">
          <el-input v-model="form.dbQixian" placeholder="请输入担保期限" />
        </el-form-item>
        <el-form-item label="工期" prop="xmGongqi">
          <el-input v-model="form.xmGongqi" placeholder="请输入工期" />
        </el-form-item>
        <!-- <el-form-item label="出函银行" prop="xmChBank">
          <el-input v-model="form.xmChBank" placeholder="请输入出函银行" />
        </el-form-item> -->
        <el-form-item label="机构对接人" prop="duijieren" :rules="{ required: false, message: '请输入', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.duijieren" placeholder="请选择机构对接人" @change="changContactPersons">
            <el-option v-for="person in contactPersons" :key="person.name"
              :label="person.name" :value="person.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="出函机构"  prop="xmChDjuser" :rules="{ required: false, message: '请输入', trigger: 'blur' }">
          <el-select class="my_select" v-model="form.xmChDjuser" placeholder="请选择出函机构">
            <el-option v-for="(bLetter,index) in bLetters" :key="bLetter.id"
              :label="bLetter.institution + '(' + bLetter.name + ')'" :value="bLetter.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="中标时间" prop="xmDate">
          <el-date-picker clearable
                          v-model="form.xmDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择中标时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="其它备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入其它备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 详情弹窗 -->
    <order-detail :form="detailform" :detailOpen="detailOpen" @changeDialog="changeDialog" />
  </div>
</template>

<script>
import {listOrder, getOrder, tijiao, delOrder, addOrder, updateOrder, zhuanOrder} from "@/api/guarantee/order";
import OrderDetail from '@/views/guarantee/order/orderDetail'
import { getBLetterList, getDuiije } from "@/api/guarantee/letter";

export default {
  name: "Order",
  components: {
    OrderDetail
  },
  data() {
    return {
      // 机构对接人
      contactPersons:[],
      //出函机构及负责人
      bLetters: [],
      statusDict: {
        '0': '草稿',
        '2': '待审核',
        '4': '完成',
        '6': '已转入',
        '-1': ' 驳回'
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 询单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        syName: null,
        syQyxz: null,
        bbzrName: null,
        bbzrQyxz: null,
        bbzrZczj: null,
        bbzrDate: null,
        dyzz: null,
        zgzz: null,
        bhType: null,
        bhGs: null,
        bhMoney: null,
        xmName: null,
        dbQixian: null,
        xmDate: null,
        xmGongqi: null,
        xmChBank: null,
        duijieren: null,
        xmChDjuser: null,
        checkUserId: null,
        checkTime: null,
        checkMind: null,
      },
      // 表单参数
      form: {
        xmChDjuser: ''
      },
      // 详情弹窗
      detailOpen: false,
      detailform: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getContactPersons();
  },
  methods: {
    /** 获取机构对接人 */
    getContactPersons(){
      getDuiije().then(response => {
        this.contactPersons = response.data;
      });
    },

    /** 修改机构对接人 */
    changContactPersons(val){
      this.form.xmChDjuser = ''
      this.getBLetters(val)
    },

    getBLetters(name) {
      getBLetterList({name}).then(response => {
        this.bLetters = response.data;
      });
    },

    // /** 修改出函机构 */
    // changInstitution(val) {
    //   let selectbLetterArr = this.bLetters.filter(item => item.id === val)
    //   if (selectbLetterArr && selectbLetterArr.length > 0) {
    //     this.selectbLetter = selectbLetterArr[0]
    //   }
    // },

    /** 查询询单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        syName: null,
        syQyxz: null,
        bbzrName: null,
        xmChDjuser: null,
        bbzrQyxz: null,
        bbzrZczj: null,
        bbzrDate: null,
        dyzz: null,
        zgzz: null,
        bhType: null,
        bhGs: null,
        bhMoney: null,
        xmName: null,
        dbQixian: null,
        xmDate: null,
        xmGongqi: null,
        xmChBank: null,
        remark: null,
        checkUserId: null,
        checkTime: null,
        checkMind: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加询单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改询单";
      });
    },
    /** 转入按钮操作 */
    handleZhuan(row) {
      this.reset();
      const id = row.id;
      this.$modal.confirm('是否确认转入项目为"' + row.xmName + '"的数据项？').then(function() {
        return zhuanOrder(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("转入成功");
      }).catch(() => {});
    },
    /** 详情按钮操作 */
    openDetail(row) {
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.detailform = response.data;
        this.detailOpen = true;
      });
    },
    changeDialog(val) {
      this.detailOpen = val;
    },
    /** 提交按钮操作 */
    handleTijiao(row) {
      this.$modal.confirm('是否确认提交履约保函项目为为"' + row.xmName + '"的数据项？').then(function () {
        return tijiao(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("提交成功");
      }).catch(() => { });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除询单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('guarantee/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
